package com.ylz.sjzx.sio.sdk.client.utils;

import com.ylz.sjzx.sio.common.core.constant.Const;
import com.ylz.sjzx.sio.common.core.enums.MessageType;
import com.ylz.sjzx.sio.sdk.client.engine.ChatEngine;
import com.ylz.sjzx.sio.sdk.client.engine.ChatEngineManager;
import com.ylz.sjzx.sio.sdk.client.session.IMSession;
import com.ylz.sjzx.sio.sdk.model.BaseSocketModel;
import com.ylz.sjzx.sio.sdk.model.ChatRoomMessage;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wiqi
 * @version 1.0
 * @date 2020/12/8
 */
@Slf4j
public class ChatRoomMessageUtil {

    /**
     * 通用发消息功能
     *
     * @param session
     * @param type
     * @param message
     */
    public static void sendMessage(IMSession session, MessageType type, String message) {
        ChatRoomMessage chatRoomMessage = null;
        switch (type) {
            case GROUP_ADMIN:
                chatRoomMessage = getMessage(session);
                break;
            case ADMIN:
                chatRoomMessage = getAdminMessage(session);
                break;
            case ALL:
                chatRoomMessage = getAllMessage(session);
                break;
            default:
                break;
        }
        chatRoomMessage.setMessage(message);
        ChatEngine chatEngine =
                ChatEngineManager.alwaysGetChatEngine(chatRoomMessage.getRoomName(), chatRoomMessage.getSocketModel());
        chatEngine.sendMessage(chatRoomMessage);
    }

    /**
     * 通用发消息功能
     *
     * @param session
     * @param type
     * @param baseSocketModel
     */
    @SneakyThrows
    public static void sendMessage(IMSession session, MessageType type, BaseSocketModel baseSocketModel) {
        ChatRoomMessage chatRoomMessage = null;
        switch (type) {
            case GROUP_ADMIN:
                chatRoomMessage = getMessage(session);
                break;
            case ADMIN:
                chatRoomMessage = getAdminMessage(session);
                break;
            case ALL:
                chatRoomMessage = getAllMessage(session);
                break;
            default:
                break;
        }
        if (baseSocketModel.isNeedtoParam()) {
            chatRoomMessage.setMessage(baseSocketModel.getParam());
        } else {
            chatRoomMessage.setMessage(baseSocketModel.toMessage());
        }
        chatRoomMessage.setCmd(baseSocketModel.getAction());
        sendAndbroadcastMessageByChatRoomMessage(chatRoomMessage);
    }

    /**
     * 发送消息根据 同时广播ChatRoomMessage
     *
     * @param chatRoomMessage
     */
    public static long sendAndbroadcastMessageByChatRoomMessage(ChatRoomMessage chatRoomMessage) {
        return onlySendMessageByChatRoomMessage(chatRoomMessage);
    }

    /**
     * 只是本机消息发送消息根据 ChatRoomMessage
     *
     * @param chatRoomMessage
     */
    public static long onlySendMessageByChatRoomMessage(ChatRoomMessage chatRoomMessage) {
        ChatEngine chatEngine =
                ChatEngineManager.alwaysGetChatEngine(chatRoomMessage.getRoomName(), chatRoomMessage.getSocketModel());
        return chatEngine.sendMessage(chatRoomMessage);
    }


    /**
     * 组内广播 同时广播admin
     *
     * @param session
     * @return
     */
    public static ChatRoomMessage getSelfMessage(IMSession session) {
        return ChatRoomMessage.builder().userId(session.getUserId())
                .socketModel(session.getSocketModel())
                .realname(session.getRealname())
                .roomName(session.getRoomName())
                .groupName(session.getGroupName())
                .role(session.getRole())
                .sid(session.getSid())
                .hostIp(session.getHostAddress())
                .build();
    }

    public static ChatRoomMessage getSingleMessage(IMSession session) {
        return ChatRoomMessage.builder().userId(session.getUserId())
                .socketModel(session.getSocketModel())
                .realname(session.getRealname())
                .roomName(session.getRoomName())
                .groupName(session.getGroupName())
                .role(session.getRole())
                .sid(session.getSid())
                .hostIp(session.getHostAddress())
                .single(true)
                .build();
    }

    /**
     * 组内广播 同时广播admin
     *
     * @param session
     * @return
     */
    public static ChatRoomMessage getMessage(IMSession session) {
        return ChatRoomMessage.builder().userId(session.getUserId())
                .socketModel(session.getSocketModel())
                .realname(session.getRealname())
                .roomName(session.getRoomName())
                .groupName(session.getGroupName())
                .role(session.getRole())
                .toGroupName(session.getGroupName())
                .sid(session.getSid())
                //目前来看 聊天室消息一定向管理组广播 管理组包括老师 辅师
                .toAdmin(true)
                .build();
    }

    /**
     * 向管理组发送
     *
     * @param session
     * @return
     */
    public static ChatRoomMessage getAdminMessage(IMSession session) {
        return ChatRoomMessage.builder().userId(session.getUserId())
                .socketModel(session.getSocketModel())
                .realname(session.getRealname())
                .roomName(session.getRoomName())
                .groupName(session.getGroupName())
                .role(session.getRole())
                .toGroupName(Const.ADMIN)
                .sid(session.getSid())
                .toAdmin(false)
                .build();
    }

    /**
     * 向全体发送
     *
     * @param session
     * @return
     */
    public static ChatRoomMessage getAllMessage(IMSession session) {
        return ChatRoomMessage.builder().userId(session.getUserId())
                .socketModel(session.getSocketModel())
                .realname(session.getRealname())
                .roomName(session.getRoomName())
                .groupName(session.getGroupName())
                .role(session.getRole())
                .toGroupName(null)
                .sid(session.getSid())
                .toAdmin(false)
                .build();
    }


}
